package org.monster.context.core.processor;

import org.monster.context.core.communicator.Header;
import org.monster.context.core.headerfilter.BaseFilterChain;
import org.monster.context.core.headerfilter.HeaderFilterChain;
import org.monster.context.core.parameter.HttpResponse;
import org.monster.context.core.request.HttpRequest;
import org.monster.context.core.request.RequestContext;
import org.monster.context.core.response.ResponseContext;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 处理请求
 *
 * @author Monster
 */
public class ProcessRequest {

    /**
     * 日志器
     */
    private static final Logger logger = Logger.getLogger(ProcessRequest.class.getName());
    /**
     * 头
     */
    private final Header header;
    /**
     * 请求
     */
    private final HttpRequest request;
    /**
     * 响应
     */
    private final HttpResponse response;

    /**
     * @param header   头
     * @param request  请求
     * @param response 响应
     */
    public ProcessRequest(Header header, HttpRequest request,
                          HttpResponse response) {
        this.header = header;
        this.request = request;
        this.response = response;
    }

    /**
     * 获取 请求信息
     *
     * @return 请求信息
     */
    public String getRequestInfo() {
        try {
            String headerInfo;
            headerInfo = request.parseRequest();
            int index = -1;
            if (headerInfo != null) {
                index = headerInfo.indexOf("\r\n");
            }
            if (headerInfo != null && index != -1) {
                String footInfo = headerInfo.substring(index,
                        headerInfo.length());
                String uri = headerInfo.substring(0, index);
                if (uri.contains("%")) {
                    uri = URLDecoder.decode(uri, "UTF-8");
                }
                String decoded = URLDecoder.decode(footInfo, "UTF-8");
                headerInfo = uri + decoded;
            }
            return headerInfo;
        } catch (IOException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * 处理 基础
     *
     * @param headerInfo 头信息
     */
    public synchronized void processBase(String headerInfo) {
        if (headerInfo != null) {
            filterControl(header);
            String method = request.getMethod();
            if (method != null && request.getURI() != null) {
                processHeader(request, response);
            }
            request.setResponse(response);
            RequestContext context = new RequestContext();
            context.process(method, request, response);
        }
    }

    /**
     * 过滤 控制
     *
     * @param header 头
     */
    private void filterControl(Header header) {
        BaseFilterChain baseChain = new HeaderFilterChain();
        header.processBaseHeader(request, response, baseChain);
    }

    /**
     * 处理 过滤器Http
     *
     * @param request  请求
     * @param response 响应
     */
    private void processHeader(HttpRequest request, HttpResponse response) {
        HeaderFilterChain chain = new HeaderFilterChain();
        header.doHeaderFilter(request, response, chain);
    }

    /**
     * 处理 响应
     *
     * @param request  请求
     * @param response 响应
     * @throws ClassNotFoundException 类未找到异常
     */
    public void processResponse(HttpRequest request, HttpResponse response)
            throws ClassNotFoundException {
        ResponseContext context = new ResponseContext();
        context.response(request, response);
    }

}
